﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace craigsPack
{
    /// <summary>
    /// Represents a custom constructor to build and modify URIs
    /// </summary>
    public class UrlBuilder
    {
        private int counter = 0;

        protected UrlBuilder(QueryStringBuilder qb)
        {
            QueryBuilder = qb;
            Paths = new string[10];
        }

        /// <summary>
        /// Adds a path to the end of the url
        /// </summary>
        /// <param name="path"></param>
        public void Append(string path)
        {
            Paths[counter++] = '/' + path;
        }
        /// <summary>
        /// Returns all the url paths in a url safe form
        /// </summary>
        /// <returns></returns>
        protected string GetUrlPath()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in Paths)
            {
                sb.Append(item);
            }
            return sb.ToString();
        }
        /// <summary>
        /// Gets the current domain name
        /// </summary>
        public string Domain { get; protected set; }
        /// <summary>
        /// Gets the current sub domain
        /// </summary>
        public string SubDomain { get; protected set; }
        /// <summary>
        /// Gets complete url with query string
        /// </summary>
        public virtual string Url
        {
            get
            {
                return "http://" + (SubDomain != null ? SubDomain + '.' : null) + Domain + '/' + "search" + GetUrlPath() + '?' + QueryBuilder.Query;
            }
        }
        /// <summary>
        /// Gets a QueryBuilder
        /// </summary>
        public QueryStringBuilder QueryBuilder { get; protected set; }
        /// <summary>
        /// Gets a url fragments
        /// </summary>
        protected string[] Paths { get; private set; }
    }
}
